package com.managertrade.config;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;

@Component
public class SqliteTableInitializer implements CommandLineRunner {

    private final DataSource sqliteDataSource;

    public SqliteTableInitializer(@Qualifier("sqliteDataSource") DataSource sqliteDataSource) {
        this.sqliteDataSource = sqliteDataSource;
    }

    @Override
    public void run(String... args) throws Exception {
        ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
        populator.addScript(new ClassPathResource("sqlite-schema.sql"));
        populator.execute(sqliteDataSource);
    }
}
